package cn.dujc.citiesocial;

import android.os.Bundle;
import android.text.TextUtils;

import net.latipay.mobile.AlipayRequest;
import net.latipay.mobile.LatipayAPI;
import net.latipay.mobile.LatipayListener;
import net.latipay.mobile.PaymentStatus;
import net.latipay.mobile.WechatpayRequest;

import java.util.HashMap;

import cn.jpush.android.api.JPushInterface;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugins.GeneratedPluginRegistrant;

public class MainActivity extends FlutterActivity {

    private String mDeviceId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
        initMethodChannel();
    }

    //实现Android和flutter交互的方案
    private void initMethodChannel(){
        new MethodChannel(getFlutterView(),"cn.dujc.citiesocial").setMethodCallHandler(new MethodChannel.MethodCallHandler() {
            @Override
            public void onMethodCall(MethodCall methodCall, MethodChannel.Result result) {
                if (methodCall.method.contentEquals("getDeviceId")){
                    if (TextUtils.isEmpty(mDeviceId)){
                        mDeviceId = JPushInterface.getUdid(getApplicationContext());
                    }
                    result.success(mDeviceId);
                }else if (methodCall.method.contentEquals("alipay")){
                    String amount = methodCall.argument("amount");
                    String merchantReference = methodCall.argument("merchantReference");
                    String productName  = methodCall.argument("productName");
                    String callbackUrl  = methodCall.argument("callbackUrl");
                    alipay(amount,merchantReference,productName,callbackUrl, result);
                }else if (methodCall.method.contentEquals("wechat")){
                    String amount = methodCall.argument("amount");
                    String merchantReference = methodCall.argument("merchantReference");
                    String productName  = methodCall.argument("productName");
                    String callbackUrl  = methodCall.argument("callbackUrl");
                    wechat(amount,merchantReference,productName,callbackUrl, result);
                }
            }
        });
    }

    private void wechat(String amount, String merchantReference, String productName, String callbackUrl, final MethodChannel.Result flutterResult){
        WechatpayRequest req = new WechatpayRequest(this);
        req.amount = amount;
        req.merchantReference = merchantReference; //must be unique in your system
        req.productName = productName; //optional
        req.callbackUrl = callbackUrl;

        req.setListener(new LatipayListener() {
            @Override
            public void onTransactionCompleted(HashMap<String, String> transaction, Error error) {
                //1. create a latipay transaction, the status is PENDING.
            }

            @Override
            public void onPaymentCompleted(int result) {
                //2. then wechat app will tell you the result of payment, the status could be PAID, UNPAID, or NEED_QUERY_SERVER
                //If it's NEED_QUERY_SERVER, please query from your own server.
                flutterResult.success(resultStr(result));
            }
        });

        LatipayAPI.sendRequest(req);
    }

    private void alipay(String amount, String merchantReference, String productName, String callbackUrl, final MethodChannel.Result flutterResult){
        AlipayRequest req = new AlipayRequest(this);
        req.amount = amount;
        req.merchantReference = merchantReference; //must be unique in your system
        req.productName = productName; //optional
        req.callbackUrl = callbackUrl;

        req.setListener(new LatipayListener() {
            @Override
            public void onTransactionCompleted(HashMap<String, String> transaction, Error error) {
                //1. create a latipay transaction which is pending, the status is PENDING.
            }

            @Override
            public void onPaymentCompleted(int result) {
                //2. then alipay app will tell you the result of payment, the status could be PaymentStatus.PAID, PaymentStatus.UNPAID, or PaymentStatus.NEED_QUERY_SERVER
                //If it's NEED_QUERY_SERVER, please query from your own server.
                flutterResult.success(resultStr(result));
            }
        });

        LatipayAPI.sendRequest(req);
    }

    private static String resultStr(int result){
        if (result == PaymentStatus.PAID) return "paid";
        else if (result == PaymentStatus.UNPAID) return "unpaid";
        else if (result == PaymentStatus.NEED_QUERY_SERVER) return "server";
        else return "pending";
    }
}
